#include "stdafx.h"

#include <myprogress.h>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <filedownloader.h>
#include <DownloadManager.h>
 myprogress ::myprogress(QWidget* parent):QWidget(parent)
{
	 setUI();
     setupConnections();
}
 void myprogress::setUI()
 {
     setWindowTitle("我的进度条");
     setFixedSize(400, 200);
     QVBoxLayout* mainLayout = new QVBoxLayout(this);

     // 嵌入式进度条
     QLabel* embeddedLabel = new QLabel("总体进度:");
     embeddedProgress = new QProgressBar();
     embeddedProgress->setRange(0, 100);
     embeddedProgress->setValue(0);
     embeddedProgress->setFormat("总体进度: %p%");

     QHBoxLayout* QGBl = new QHBoxLayout();
     start = new QPushButton("开始");
     reset = new QPushButton("重置");

     QGBl->addWidget(start);
     QGBl->addWidget(reset);

     mainLayout->addLayout(QGBl);
     mainLayout->addWidget(embeddedProgress);
     taskTimer = new QTimer(this);
    

 }

 void myprogress::setupConnections()
 {
 
     //  qDebug() << "按钮被点击了!";
    
     connect(start, &QPushButton::clicked, this, &myprogress::startProgress);
     connect(reset, &QPushButton::clicked, this, &myprogress::resetProgress);
     connect(taskTimer, &QTimer::timeout, this, &myprogress::updateProgress);

 }



 void myprogress::startProgress()
 {
   /*  FileDownloader* downloader = new FileDownloader(this);
     downloader->download(QUrl("https://cdn-lfs-cn-1.modelscope.cn/prod/lfs-objects/64/9e/136c731c1a1ab7f9af8b4a465404159f25c19d3affc2883cc2453e09c638?filename=model.onnx&namespace=manyeyes&repository=alicttransformerpunc-large-zh-en-onnx&revision=master&tag=model&auth_key=1759635774-f6f68f2fe08e41cfbfdb1715398064db-0-303da056226deb9aa47df6ade82a0280"),
         "D:\\123pan\\1.onnx");*/
     DownloadManager  download;
     download.startDownload("https://sfdl.360safe.com/360zip_setup.exe", "D:/123pan/Blender4.5/360zip_setup.exe");
     // 正确的信号槽连接方式
 /*    connect(downloader, &FileDownloader::progress, this, [=](qint64 bytesReceived, qint64 bytesTotal) {
        
         if (bytesTotal > 0) {
             double percent = (static_cast<double>(bytesReceived) / bytesTotal) * 100.0;
             int progress = static_cast<int>(percent);
             embeddedProgress->setValue(progress);
         }
     });*/

    
  //  embeddedProgress->setValue(0);
     

    // taskTimer->start(100);

 }
 void myprogress::updateProgress()
 {
     // QTimer* taskTimer = new QTimer(this);
     qDebug() << "按钮1被点击了!";
   


     progressValue += 2;
     embeddedProgress->setValue(progressValue);
     if (progressValue >= 100) {
         taskTimer->stop();
         //taskTimer->deleteLater();
        // taskTimer = nullptr;
     }

 }

 void myprogress::resetProgress()
 {
    // progressValue = 0;
     taskTimer->stop();
     // QTimer* taskTimer = new QTimer(this);
     embeddedProgress->setValue(0);

 }
